package seqList;

import operation.LList;

public  class SeqList<T> implements LList<T> {
	private Object[] element;
	private int len;
	
	public SeqList(int size)
	{
		this.element=new Object[size];
		this.len=0;
	}
	
	public SeqList()
	{
		this(64);
	}
	public SeqList(Integer[] random){
		
	}
	
	public boolean isEmpty() {
		return this.len==0;
	}
 
	public int length() {
		return this.len;
	}
 
	public T get(int i) {
		if(i>=0&&i<this.len){
			//System.out.println("position="+i+",is:"+this.element[i]);
			return (T) this.element[i];	
	}
	return null;
}
 
	public T get(T obj){
		return null;
	}
	public void set(int i, T x) {
		if(x==null)
			return;
		if(i>=0&&i<this.len)
			this.element[i]=x;
		else throw new IndexOutOfBoundsException(i+"");
	}
	
	public String toString()
	{
		String str="(";
		if(len>0)
			str+=element[0].toString();
		for(int i=1;i<len;i++)
			str+=","+element[i].toString();
		return str+")";
	}
	public void insert(int i, T x) {
		if(x==null)
			return;
		if(i<0)
			i=0;
		if(i>this.len)
			i=this.len;
		if(this.len==element.length)
		{
			Object[] temp =this.element;
			this.element = new Object[element.length*2];
			for(int j=0;j<temp.length;j++)
				this.element[j]=temp[j];
		}
		//System.out.println("insert a new node:"+x+",in position"+i);
		for(int j=this.len-1;j>=i;j--)
			this.element[j+1]=this.element[j];
		this.element[i]=x;
		this.len++;
			
	}
 
	public void append(T x) {
		
		insert(this.len,x);
	}
 
 
	public int  search(T key) {
		for(int i=0;i<this.len;i++)
			if(key.equals(this.element[i])){
				return i;
		
			}		
		return -1;
	}

	

	public boolean contain(T x) {
			return false;

	}

	public void showListInfo() {
		System.out.println("seqlist:"+this.toString()+",length="+this.len);

	}
	public T[]getElements(){
		return (T[]) this.element;
	}
}
